package com.wjjm.spoonselling.controller;

import com.wjjm.spoonselling.pojo.Cart;
import com.wjjm.spoonselling.service.CartService;
import com.wjjm.spoonselling.util.Code;
import com.wjjm.spoonselling.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("cart")
public class CartController {

    @Autowired
    CartService cartService;

    @PostMapping("/insert")
    public Result insert(@RequestBody Cart cart) {
        Boolean insert = cartService.insert(cart);
        Result result = new Result();
        if (insert) result.setCode(Code.ADD_OK);
        else result.setCode(Code.ADD_ERR);
        if (insert) result.setMsg("成功加入购物车");
        else result.setMsg("加入购物车失败");

        return result;
    }

    @GetMapping("/selectByC/{customerId}")
    public Result selectByCustomerId(@PathVariable int customerId) {
        List<Cart> carts = cartService.selectByCustomerId(customerId);
        int code = carts != null ? Code.SELECT_OK : Code.SELECT_ERR;
        String msg = carts != null ? "数据查询成功" : "数据查询失败，请重试";
        return new Result(code, carts, msg);
    }
}
